<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
     public function __construct(){
        parent::__construct();
        $this->load->library('session');
    }

    /**
     * 验证码
     */
    public function captcha(){
        $this->load->helper('captcha');
        $vals = array(
            'word'      => '',
            'img_path'  => './captcha/',
            'img_url'   => base_url('/captcha/'),
            'font_path' => './public/fonts/droidsansmono__.ttf',
            'img_width' => '277',
            'img_height'    => 60,
            'expiration'    => 1,
            'word_length'   => 6,
            'font_size' => 25,
            'img_id'    => 'Imageid',
            'pool'      => '0123456789abcdefghijklmnopqrstuvwxyz',

            // White background and border, black text and red grid
            'colors'    => array(
                'background' => array(255, 255, 255),
                'border' => array(215, 255, 255),
                'text' => array(0, 0, 0),
                'grid' => array(255, 40, 40)
            )
        );

        $cap = create_captcha($vals);

        //将验证码值转换为一次性
        $this->session->set_userdata('word', $cap['word']);
        $this->session->mark_as_flash('word');


        return $cap;
    }

    /**
     * 刷新验证码
     */
    public function reset_cap(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $captcha = $this->captcha();

        echo $captcha['image'];
    }

    public function index()
    {
        $suser = $this->session->suser;

        if(!empty($suser)){
            redirect(site_url('shops/home'));
        }else{
            //用户登录验证
            if (!$this->input->is_ajax_request()){
                //获取验证码
                $captcha = $this->captcha();
                $data = ['cap' => $captcha['image']];
                $this->load->view('shops/login_index',$data);
            }else{
                //登录
                $post = $this->input->post(NULL,TRUE);
                //判断验证码
                $word = empty($_SESSION['word']) ? '' : $_SESSION['word'];
                if($word != $post['cap']){
                    die('验证码错误！');
                }

                $this->load->model('suser_mdl');
                $this->db->select('id,store_id,permissions,username,phone,status,login_num');
                $this->db->where('username',$post['username']);
                $this->db->where('password',md5($post['password']));

                $res = $this->suser_mdl->get_one();

                if(empty($res)){
                    die('帐号或者密码错误！');
                }

                if($res['status'] == 2){
                    die('帐号或者密码错误！');
                }

                //查询商铺信息
                $this->load->model('store_mdl');
                $this->db->where('id',$res['store_id']);
                $store = $this->store_mdl->get_one();
                
                if(empty($store)){
                    die('帐号或者密码错误！');
                }

                //更新数据库
                $data = [
                    'end_time' => $_SERVER['REQUEST_TIME'],
                    'login_num' => $res['login_num']+1
                ];
                $this->db->where('id',$res['id']);
                $this->suser_mdl->update($data);

                //写入session
                $this->session->set_userdata('store',$store);
                $this->session->set_userdata('suser',$res);

                echo '1';
            }
        }
    }

    public function suser(){

    }

}
